<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* MODULE NAME   : Bb_vehicle.php
*
* DESCRIPTION   : Bb_vehicle module controller
*
* MODIFICATION HISTORY
*   V1.0   2007-04-21 12:01 PM   - Dennis T. Kaplan     - Created
*
* @package             Bb_vehicle
* @subpackage          bb_vehicle component Class
* @author              Dennis T. Kaplan
* @copyright           Copyright (c) 2006-2007 PzzAzz
* @since               Version 1.0
* @filesource
*/

class Bb_vehicle extends Controller 
{
    function setVars()
    {
		$this->tpl['title'] = 'Billboard Vehicle';
        $this->tpl['head'] = '';
		$this->uid = $this->session->userdata('uid');
        $this->table = 'bb_vehicle';
	}
    
    function __construct() {
        parent::Controller();
        $this->load->library('profiler');
        $this->auth->CheckTaMeR(11,100);
        $this->setVars();
        $this->load->model('Master_Model', 'mdb');
        $this->load->model('bb_vehiclemodel', 'bbm');
    }
    
    function index() {
        $this->browse();
    }
    
    function browse() 
    {
        $tbresult = $this->bbm->get_by_oid();  
        if($tbresult !== FALSE){
            $this->load->library('table');
            foreach($tbresult as $row){
                $v = $row['id'];
                $n = $row['Title'];
                $img = $row['Image'];
                $this->load->helper('img');
                $row['Image'] = '<img src="/img/vehicle/'.$img.'" alt="n/a" />';
                $row['Action'] = anchor_icon('billboard/bb_vehicle/vehicle_view/'.$v,'actions/edit-find', 'View');
                $row['Action'] .= ' | ' . anchor_icon('billboard/bb_vehicle/modify/'.$v,'actions/document-new','Add/Edit');
                $row['Action'] .= ' | ' . anchor_icon('billboard/bb_vehicle/addImage/'.$v.'/'.$n, 'devices/camera-photo');
                unset($row['id']);
                $this->table->add_row($row);
            }
            $this->table->set_heading(array_keys($row));            
            $this->tpl['body'] = $this->table->generate();
            $this->table->clear();
            $this->load->view('blank', $this->tpl);
        }else{
            redirect('dbview/pages/error_db', 'location');
        }
    }

    
    function add() {
        if( isset($_POST['submit_vehicle'] ) ) {
            $vdata = $this->postCheck();
            
            $this->bbm->add($vdata);
            redirect('billboard/bb_vehicle', 'location');
        } else {
            $this->load->model('contact_orgmodel');
            $this->Form();
        }
    }
    
    function modify() 
    {
        if( isset($_POST['submit_vehicle'] ) ) {
            $vdata = $this->postCheck();
            $this->bbm->modify($vdata['id'], $vdata);
            redirect('billboard/bb_vehicle', 'location');
        }
        else {
            $this->Form();
        }
    }

	function addImage()
	{
        $this->tpl['error'] = '';
        $this->load->view('billboard/upload_form',  $this->tpl );        
    }
    
	function uploadImage()
	{
        $this->load->library('upload');
        $config['upload_path'] = 'img/vehicle/';
        $config['overwrite'] = TRUE;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['encrypt_name'] = TRUE;
        $config['max_size']	= '1024'; # 1024 = 1MB
        $config['max_width']  = '1024';
        $config['max_height']  = '800';
        $this->upload->initialize($config);
        
        if ( ! $this->upload->do_upload()){
            $this->tpl['error'] = $this->upload->display_errors();
            $this->load->view('billboard/upload_form', $this->tpl);
        }else{
            $id     = $this->input->post('id', TRUE);
            $name   = $this->input->post('Title', TRUE);

            $this->tpl['upload_data'] = $this->upload->data();
            $new_file = $this->tpl['upload_data']['file_path'];
            $new_file .= $name;
            $new_file .= $this->tpl['upload_data']['file_ext'];
            rename($this->tpl['upload_data']['full_path'], $new_file);
            $config['image_library'] = 'GD2';
            $config['source_image'] = $new_file;
            $config['create_thumb'] = TRUE;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 100;
            $config['height'] = 100;
            $this->load->library('image_lib', $config);
            if ( ! $this->image_lib->resize()){
                $error = $this->image_lib->display_errors();
                $this->msg->setError($error);
            }else{
                $config['create_thumb'] = FALSE;
                $config['width'] = 500;
                $config['height'] = 500;
                $this->image_lib->initialize($config);
                if ( ! $this->image_lib->resize()){
                    $error = $this->image_lib->display_errors();
                    $this->msg->setError($error);
                }else{
                    $img = $Title.'_thumb'.$this->tpl['upload_data']['file_ext'];
                    $this->bbm->edit_image($id, array('Image'=> $img));
                }
            }
            redirect('dbview/pages/success_upload', 'location');
        }
	}	

    
    function Form()
    {
        $action = $this->uri->segment(3);
        $id = $this->uri->segment(4);
        
        if( ! isset($this->dbdata)) {
            if($id < 1) $id = FALSE;
            $this->dbdata = $this->mdb->initdb($id);
        }
        extract($this->dbdata);
        if( $Showroom == '1'  || $Showroom == 'Yes' ){ 
            $checkYes = TRUE; 
            $checkNo = FALSE;
        }else{
            $checkYes = FALSE; 
            $checkNo = TRUE;
        }
        $org_id = $this->mdb->get_Org_id();        
        
        $attributes = array('name' => 'vehicle');
        $this->load->helper('form');
        $this->tpl['body'] =  form_open("billboard/bb_vehicle/$action", $attributes);
        $data = array(
            'name'        => 'Title',
            'id'          => $id,
            'value'       => $Title,
            'maxlength'   => '30',
            'size'        => '10',
            'style'       => ''
            );
        $form['Title'] = form_input($data);
        $form[1]['Has Showroom'] = form_radio('Showroom', 'Yes', $checkYes) . 'Yes ' . form_radio('Showroom', 'No', $checkNo) . 'No';
        $this->load->library('table');
        $this->tpl['body'] .= $this->table->form_table($form);
        $this->tpl['body'] .= form_hidden('id', $id);
        $this->tpl['body'] .= form_hidden('org_id', $org_id);
        $this->tpl['body'] .= '<div style="text-align: right;"><input type="submit" name="submit_vehicle" value="Submit" /></div>';
        $this->tpl['body'] .= '</form>';
        $this->load->view('blank', $this->tpl);
    } 
    
   /**
   * postCheck function
   *
   * Checks user posts for database insert and update. 
   *
   * @access      private
   * @return      all db fields updated from validation as array
   */
    private function postCheck()
    {
        $this->load->library('validation');
        $rules['id']  = "trim|max_length[4]|numeric|xss_clean";
        $rules['Title']		= "trim|required|min_length[4]|max_length[30]|alpha_dash|xss_clean";
        $rules['Showroom']  = "trim|required|max_length[3]";
        $this->validation->set_rules($rules);
        if ($this->validation->run()) {
            if( ! isset($this->dbdata)) {
                if(isset($this->validation->id)){
                    $id = $this->validation->id;
                }else{
                    $id = FALSE;
                }
                $this->dbdata = $this->mdb->initdb($id);
                $this->dbdata['org_id'] = $this->mdb->get_Org_id();
            }
            $vdata = (array) $this->dbdata;
            foreach($this->dbdata as $k => $v){
                if(isset($this->validation->$k)){
                    $vdata[$k] = $this->validation->$k;
                }
            }
            return $vdata;
        }
    }
    
    function delete() {
        $idField = $this->uri->segment(3);
        $the_results = $this->bbm->delete_by_pkey($idField);
        redirect('bb_vehicle/', 'location');
        
    }
    
    function vehicle_view()
    {
        $vid = $this->uri->segment(4);
        $this->db->select('id, Title, Showroom, Image');
        $query = $this->db->getwhere($this->table, array('id' => $vid));
        if ($query->num_rows() > 0)
        {
            $v = $query->row();
            $this->load->library('table');

            $form['Image'] = '<img src="/img/vehicle/'.$v->Image.'" alt="n/a" />';
            $form[1]['id'] = $v->id;
            $form[1]['Title'] = $v->Title;
            $form[1]['Showroom'] = $v->Showroom;
            $this->tpl['sidebar'] = $this->table->form_table($form);
            $img = str_replace('_thumb','', $v->Image);
            $this->tpl['body'] = '<img src="/img/vehicle/'.$img.'" alt="n/a" />';
            
            
        }
        $this->load->view('blank', $this->tpl);
    }
    
    
    
}
?>